#include "broker.h"
#include "net.h"
#include "main.h"
#include <iostream> 
#include <sys/socket.h>
#include <sys/un.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <map>
#include <string>

using namespace std;
using net::ServerSocket;
using net::Socket;

static int BROKERS_LIMIT = 10;

int main(int count, char* args[]) {
    Context* ctx = new Context();
    cerr << "I\'ll be a broker soon :))\n";
   
    ServerSocket server(6666);
    //int socket_id = socket(PF_INET, SOCK_STREAM, 0);
    
    //cerr << "scoket id: " << socket_id << "\n";
    
    //struct sockaddr_in addr;
    //addr.sin_family = AF_INET;
    //addr.sin_port = htons(6666);
    //addr.sin_addr.s_addr = INADDR_ANY;

    //if (bind(socket_id, (sockaddr *)&addr,
    //    sizeof(struct sockaddr_in)) < 0) {
    //    cerr << "bind failed.\n";
    //    return 1;
    //}

    //cerr << "binded to port 6666\n";

    //if (listen(socket_id, 5) < 0) {
    //    cerr << "listen failed.\n";
    //    return 2;
    //}

    //struct sockaddr_in remote_addr;
    //socklen_t remote_addr_len;
  
    while (true) {
        cerr << "accepting...\n";
	Socket* socket = server.accept();
        //int csock_id = accept(socket_id, (sockaddr *)&remote_addr,
	  //  &remote_addr_len);
        //if (csock_id < 0) {
          //  cerr << "accept failed.\n";
	    //return 3;
        //}

        //char* ip = inet_ntoa(remote_addr.sin_addr);
        //cerr << "accepted from " << ip << "\n";

        ctx->enqueue(new InitServerJob(socket, ctx));
//        Broker* broker = new Broker(readId(csock_id), csock_id);
//        brokers[broker->id()] = broker;
//        broker->start();
    }
}

